import { defineStore } from 'pinia'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr/index.ts'
import type { CategoryResponseData } from '@/api/product/attr/type'
import type { CategorySate } from './types/types'

const useCategoryStore = defineStore('category', {
  state: (): CategorySate => {
    return {
      c1Arr: [],
      c1Id: '',
      c2Arr: [],
      c2Id: '',
      c3Arr: [],
      c3Id: '',
    }
  },
  actions: {
    async getC1() {
      const res: CategoryResponseData = await reqC1()
      if (res.code === 200) {
        this.c1Arr = res.data
      }
    },
    async getC2(selC1Id: number) {
      const res: CategoryResponseData = await reqC2(selC1Id)
      if (res.code === 200) {
        this.c2Arr = []
        this.c2Id = ''
        this.c3Arr = []
        this.c3Id = ''

        this.c2Arr = res.data
      }
    },
    async getC3(selC2Id: number) {
      const res: CategoryResponseData = await reqC3(selC2Id)
      if (res.code === 200) {
        this.c3Arr = []
        this.c3Id = ''
        this.c3Arr = res.data
      }
    },
  },
  getters: {},
})

export default useCategoryStore
